package org.jinyan.led.service;

import java.lang.reflect.Type;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.jinyan.db.constant.Tables;
import org.jinyan.db.dao.IDBDao;
import org.jinyan.db.dao.ISuperMapDao;
import org.jinyan.db.dao.impl.DBDao;
import org.jinyan.db.dao.impl.SuperMapDao;
import org.jinyan.db.utils.BeanMapConverUtils;
import org.jinyan.led.constant.DeviceConstants;
import org.jinyan.led.model.Device;
import org.jinyan.led.model.DeviceInstance;
import org.jinyan.led.tcp.ClientSocketManager;
import org.jinyan.utils.CommonUtils;
import org.jinyan.utils.LogSysoutUtils;

import com.google.gson.reflect.TypeToken;

public class DeviceService {

	private ISuperMapDao superMapDao = new SuperMapDao();

	/**
	 * 
	 * @param openID
	 * @param serialNumber
	 * @param alias
	 * @return
	 */
	public String bindingDevice(String openID, String serialNumber, String alias) {
		LogSysoutUtils.log("注册设备  bindingDevice start");
		String info = "";
		Device device = null;
		try {
			device = getDevice(serialNumber);
		} catch (SQLException e) {
			info = DeviceConstants.BINDING_EXCEPTION;
			e.printStackTrace();
		}
		if (device != null) {
			if (this.isExistDeviceInstance(serialNumber)) {
				updateDeviceInstance(serialNumber, alias);
			} else {
				saveDeviceInstance(openID, serialNumber, alias);
			}
			info = alias + "," + DeviceConstants.BINDING_SUCCESS;
		} else {
			info = DeviceConstants.BINDING_FAIL;
		}
		LogSysoutUtils.log("注册设备  bindingDevice end");
		return info;
	}

	/**
	 * 
	 * @param openID
	 * @param serialNumber
	 * @param alias
	 * @return
	 * @throws SQLException 
	 */
	public String removeDevice(String openID, String alias){
		LogSysoutUtils.log("移除设备  removeDevice start");
		String info = "";

		if (this.isExistDeviceInstance(openID, alias)) {
			try {
				removeDeviceInstance(openID, alias);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			info = alias + "," + DeviceConstants.REMOVE_SUCCESS;

		}else{
			info = alias + ",移除失败";
		}
		
		LogSysoutUtils.log("移除设备  removeDevice end");
		return info;
	}

	private boolean removeDeviceInstance(String openID, String alias)
			throws SQLException {
		DeviceInstance instance = new DeviceInstance();
		instance.setAlias(alias);
		instance.setOpenId(openID);
		Map instanceMap = BeanMapConverUtils.beanToMap(instance);
		return superMapDao.delete(Tables.device_instance_tb, instanceMap);

	}

	public boolean saveDeviceInstance(String openId, String serialNumber,
			String alias) {
		boolean result = false;
		DeviceInstance instance = new DeviceInstance();
		instance.setId(CommonUtils.getUUID());
		instance.setSerialNumber(serialNumber);
		instance.setOpenId(openId);
		instance.setAlias(alias);
		instance.setInsertDT(CommonUtils.convertDtToChar14(new Date()));
		Map instanceMap = BeanMapConverUtils.beanToMap(instance);
		try {
			result = superMapDao.add(Tables.device_instance_tb, instanceMap);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public boolean updateDeviceInstance(String serialNumber, String alias) {
		boolean result = false;
		DeviceInstance instance = new DeviceInstance();
		instance.setSerialNumber(serialNumber);
		instance.setAlias(alias);
		instance.setUpdateDT(CommonUtils.convertDtToChar14(new Date()));
		Map instanceMap = BeanMapConverUtils.beanToMap(instance);
		try {
			result = superMapDao.update(Tables.device_instance_tb, instanceMap,
					"serialNumber='" + serialNumber + "'");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}

	public boolean updateDeviceInstance(DeviceInstance deviceInstance) {
		boolean result = false;
		Map instanceMap = BeanMapConverUtils.beanToMap(deviceInstance);
		try {
			result = superMapDao.update(Tables.device_instance_tb, instanceMap,
					"serialNumber='" + deviceInstance.getSerialNumber() + "'");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public boolean saveDeviceInstanceHis(DeviceInstance deviceInstance) {
		boolean result = false;
		Map instanceMap = BeanMapConverUtils.beanToMap(deviceInstance);
		instanceMap.put("id", CommonUtils.getUUID());
		try {
			result = superMapDao
					.add(Tables.device_instance_his_tb, instanceMap);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	public DeviceInstance getDeviceInstance(String openID, String alias) {
		String sql = "SELECT * FROM device_instance  WHERE openId=? AND  alias =?";
		IDBDao dbDao = new DBDao();
		List list = null;
		try {
			list = dbDao.selectList(sql, new Object[] { openID, alias });
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!list.isEmpty()) {
			Map item = (Map) list.get(0);
			Type type = new TypeToken<DeviceInstance>() {
			}.getType();
			DeviceInstance device = (DeviceInstance) BeanMapConverUtils
					.mapToBean(item, type);
			return device;
		} else {
			return null;
		}
	}

	public List<DeviceInstance> getAllDeviceInstance() {
		String sql = "SELECT * FROM device_instance  WHERE  is";
		IDBDao dbDao = new DBDao();
		List list = null;
		try {
			list = dbDao.selectList(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Type type = new TypeToken<List<DeviceInstance>>() {
		}.getType();
		List<DeviceInstance> listAll = (List<DeviceInstance>) BeanMapConverUtils
				.mapToBean(list, type);
		return listAll;

	}

	public DeviceInstance getDeviceInstance(String serialNumber) {
		String sql = "SELECT * FROM device_instance  WHERE serialNumber=?";
		IDBDao dbDao = new DBDao();
		List list = null;
		try {
			list = dbDao.selectList(sql, new Object[] { serialNumber });
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!list.isEmpty()) {
			Map item = (Map) list.get(0);
			Type type = new TypeToken<DeviceInstance>() {
			}.getType();
			DeviceInstance device = (DeviceInstance) BeanMapConverUtils
					.mapToBean(item, type);
			return device;
		} else {
			return null;
		}
	}

	public Device getDevice(String serialNumber) throws SQLException {
		String sql = "SELECT * FROM device  WHERE serialNumber=?";
		IDBDao dbDao = new DBDao();
		List list = dbDao.selectList(sql, new Object[] { serialNumber });
		if (!list.isEmpty()) {
			Map item = (Map) list.get(0);
			Type type = new TypeToken<Device>() {
			}.getType();
			Device device = (Device) BeanMapConverUtils.mapToBean(item, type);
			return device;
		} else {
			return null;
		}

	}

	public boolean isExistDeviceInstance(String serialNumber) {
		String sql = "SELECT * FROM device_instance  WHERE serialNumber=?";
		IDBDao dbDao = new DBDao();
		List list = null;
		try {
			list = dbDao.selectList(sql, new Object[] { serialNumber });
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		if (!list.isEmpty()) {
			Map item = (Map) list.get(0);
			return true;
		} else {
			return false;
		}
	}

	public boolean isExistDeviceInstance(String openID, String alias) {
		String sql = "SELECT * FROM device_instance  WHERE openID=? and alias=?";
		IDBDao dbDao = new DBDao();
		List list = null;
		try {
			list = dbDao.selectList(sql, new Object[] { openID, alias });
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		if (!list.isEmpty()) {
			Map item = (Map) list.get(0);
			return true;
		} else {
			return false;
		}
	}

}
